import 'package:logger/logger.dart';
import '../constants/app_constants.dart';

class AppLogger {
  static Logger? _logger;
  
  static Logger get instance {
    _logger ??= Logger(
      printer: PrettyPrinter(
        methodCount: 2,
        errorMethodCount: 8,
        lineLength: 120,
        colors: true,
        printEmojis: true,
        printTime: true,
      ),
      output: ConsoleOutput(),
      filter: AppConstants.enableLogger ? DevelopmentFilter() : ProductionFilter(),
    );
    return _logger!;
  }

  static void verbose(dynamic message, [dynamic error, StackTrace? stackTrace]) {
    if (AppConstants.enableLogger) {
      instance.t(message, error: error, stackTrace: stackTrace);
    }
  }

  static void debug(dynamic message, [dynamic error, StackTrace? stackTrace]) {
    if (AppConstants.enableLogger) {
      instance.d(message, error: error, stackTrace: stackTrace);
    }
  }

  static void info(dynamic message, [dynamic error, StackTrace? stackTrace]) {
    if (AppConstants.enableLogger) {
      instance.i(message, error: error, stackTrace: stackTrace);
    }
  }

  static void warning(dynamic message, [dynamic error, StackTrace? stackTrace]) {
    if (AppConstants.enableLogger) {
      instance.w(message, error: error, stackTrace: stackTrace);
    }
  }

  static void error(dynamic message, [dynamic error, StackTrace? stackTrace]) {
    if (AppConstants.enableLogger) {
      instance.e(message, error: error, stackTrace: stackTrace);
    }
  }

  static void wtf(dynamic message, [dynamic error, StackTrace? stackTrace]) {
    if (AppConstants.enableLogger) {
      instance.f(message, error: error, stackTrace: stackTrace);
    }
  }

  static void network(String method, String url, {
    Map<String, dynamic>? headers,
    dynamic data,
    dynamic response,
    int? statusCode,
    Duration? duration,
  }) {
    if (AppConstants.enableLogger) {
      final buffer = StringBuffer();
      buffer.writeln('[$method] $url');
      
      if (statusCode != null) {
        buffer.writeln('Status: $statusCode');
      }
      
      if (duration != null) {
        buffer.writeln('Duration: ${duration.inMilliseconds}ms');
      }
      
      if (headers != null && headers.isNotEmpty) {
        buffer.writeln('Headers: $headers');
      }
      
      if (data != null) {
        buffer.writeln('Request: $data');
      }
      
      if (response != null) {
        buffer.writeln('Response: $response');
      }
      
      instance.d(buffer.toString());
    }
  }

  static void page(String pageName, {Map<String, dynamic>? params}) {
    if (AppConstants.enableLogger) {
      final buffer = StringBuffer();
      buffer.writeln('Page: $pageName');
      
      if (params != null && params.isNotEmpty) {
        buffer.writeln('Params: $params');
      }
      
      instance.i(buffer.toString());
    }
  }

  static void event(String eventName, {Map<String, dynamic>? params}) {
    if (AppConstants.enableLogger) {
      final buffer = StringBuffer();
      buffer.writeln('Event: $eventName');
      
      if (params != null && params.isNotEmpty) {
        buffer.writeln('Params: $params');
      }
      
      instance.i(buffer.toString());
    }
  }

  static void performance(String name, Duration duration, {Map<String, dynamic>? extras}) {
    if (AppConstants.enableLogger) {
      final buffer = StringBuffer();
      buffer.writeln('Performance: $name');
      buffer.writeln('Duration: ${duration.inMilliseconds}ms');
      
      if (extras != null && extras.isNotEmpty) {
        buffer.writeln('Extras: $extras');
      }
      
      instance.i(buffer.toString());
    }
  }

  static void lifecycle(String lifecycle, String pageName, {Map<String, dynamic>? extras}) {
    if (AppConstants.enableLogger) {
      final buffer = StringBuffer();
      buffer.writeln('Lifecycle: $lifecycle');
      buffer.writeln('Page: $pageName');
      
      if (extras != null && extras.isNotEmpty) {
        buffer.writeln('Extras: $extras');
      }
      
      instance.i(buffer.toString());
    }
  }

  static void close() {
    _logger?.close();
    _logger = null;
  }
}

class NetworkLogInterceptor {
  static void logRequest(String method, String url, {
    Map<String, dynamic>? headers,
    dynamic data,
  }) {
    AppLogger.network(
      method,
      url,
      headers: headers,
      data: data,
    );
  }

  static void logResponse(String method, String url, {
    Map<String, dynamic>? headers,
    dynamic data,
    dynamic response,
    int? statusCode,
    Duration? duration,
  }) {
    AppLogger.network(
      method,
      url,
      headers: headers,
      data: data,
      response: response,
      statusCode: statusCode,
      duration: duration,
    );
  }

  static void logError(String method, String url, {
    dynamic error,
    StackTrace? stackTrace,
    Duration? duration,
  }) {
    AppLogger.error(
      'Network Error: [$method] $url',
      error,
      stackTrace,
    );
  }
}